{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "60726cfa-e1d3-4710-803f-3affd57c0353",
   "metadata": {},
   "outputs": [],
   "source": [
    "import jieba\n",
    "import os\n",
    "from nltk.parse import stanford"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a5feda7-bf09-4ec3-8a46-a4d821ed4b56",
   "metadata": {},
   "outputs": [],
   "source": [
    "string='2022年的北京冬奥会是第24届冬季奥林匹克运动会'\n",
    "seg_list=jieba.cut(string,cut_all=False,HMM=True)\n",
    "seg_str=' '.join(seg_list)\n",
    "print('分词结果:',seg_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "676b2b37-4958-47bb-b317-b8821df7ce6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "if not os.environ.get('JAVA_HOME'):\n",
    "    JAVA_HOME='C:\\Program Files\\Java\\jdk-21'\n",
    "    os.environ['JAVA_HOME']=JAVA_HOME"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3a78c6ed-a687-4567-96f6-a7f97920089c",
   "metadata": {},
   "outputs": [],
   "source": [
    "parser_path='stanford-parser.jar'\n",
    "model_path='stanford-parser-4.2.0-models.jar'\n",
    "pcfg_path='edu/stanford/nlp/models/lexparser/chinesePCFG.ser.gz'\n",
    "parser=stanford.StanfordParser(path_to_jar=parser_path,path_to_models_jar=model_path,model_path=pcfg_path)\n",
    "sentence=parser.raw_parse(seg_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a005a719-b87a-445a-a4d2-fdfcad6b3e28",
   "metadata": {},
   "outputs": [],
   "source": [
    "for line in sentence:\n",
    "    print('句法分析的结果:\\n',line)\n",
    "    print('句法树的叶子节点：\\n',line.leaves())\n",
    "    line.draw()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e0301be-cf2a-4c68-afe9-0610bcf0e925",
   "metadata": {},
   "outputs": [],
   "source": [
    "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f34209e-1100-4d4f-bfa6-1aa40cb17a97",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
